################# This creates Fig 4 & App Fig 6: CS Estimates##########################
library(data.table)
library(dplyr)
library(lubridate)
library(tidyr)
library(readstata13)
library(did)
library(ggplot2)
library(ggpubr)
library(ggrepel)
#Download latest version of did package and load it
#devtools::install_github("bcallaway11/did")
library(did)
library(here)
library(tidyverse)
library(scales)
source("R_Code/CS_Function.R")
set.seed(1234)




###############################################################################
#                            2011 and Onwards                                 #
###############################################################################

################## Load in the data ########################
data_analysis <- read.dta13("data/primary/state_panel_mw_poverty.dta") %>%
  filter(dem==1) %>%
  group_by(state_fips) %>% 
  mutate(eff100=ifelse( mw100==0,max(mw100*10000),year*mw100) %>% min()) %>% 
  mutate(eff75=ifelse( mw75==0,max(mw75*10000),year*mw75) %>% min()) %>% 
  mutate(eff50=ifelse( mw50==0,max(mw50*10000),year*mw50) %>% min()) %>% 
  mutate(eff25=ifelse( mw25==0,max(mw25*10000),year*mw25) %>% min()) %>% 
  mutate(dhpi=ifelse(chpi>=0.05,1,0)) %>%
  arrange(state_fips, year) %>%
  group_by(state_fips) %>%
  mutate(mw_change_nom = mw - lag(mw)) %>%
  mutate(mw_change_nom = ifelse(is.na(mw_change_nom),0,mw_change_nom)) %>%
  mutate(changed5 = ifelse(mw_change_nom >= 0.05, year, 9999)) %>%
  mutate(changed10 = ifelse(mw_change_nom >= 0.10, year, 9999)) %>%
  mutate(changed25 = ifelse(mw_change_nom >= 0.25, year, 9999)) %>%
  mutate(changed50 = ifelse(mw_change_nom >= 0.5, year, 9999)) %>%
  mutate(changed75 = ifelse(mw_change_nom >= 0.75, year, 9999)) %>%
  mutate(changed100 = ifelse(mw_change_nom >= 1.00, year, 9999)) %>%
  mutate(changed125 = ifelse(mw_change_nom >= 1.25, year, 9999)) %>%
  mutate(changed110 = ifelse(mw_change_nom >= 1.10, year, 9999)) %>%
  mutate(changed90 = ifelse(mw_change_nom >= 0.90, year, 9999)) %>%
  mutate(changed80 = ifelse(mw_change_nom >= 0.80, year, 9999)) %>%
  group_by(state_fips) %>% 
  mutate(eff_nom_5 = min(changed5)) %>%
  mutate(eff_nom_10 = min(changed10)) %>%
  mutate(eff_nom_25 = min(changed25)) %>%
  mutate(eff_nom_50 = min(changed50)) %>%
  mutate(eff_nom_75 = min(changed75)) %>%
  mutate(eff_nom_100 = min(changed100)) %>%
  mutate(eff_nom_80 = min(changed80)) %>%
  mutate(eff_nom_90 = min(changed90)) %>%
  mutate(eff_nom_110 = min(changed110)) %>%
  mutate(eff_nom_125 = min(changed125)) %>%
  mutate(y=under_100)


##################  RUN CS Estimates: #############################

for (inc in c(50, 75, 100)) {
  tryCatch({
    label="Overall"
    
    xmin = -4
    xmax = 3
    if (inc==50) {
      ymin = -0.1
      ymax = 0.1
    } else if (inc==75) {
      ymin = -0.075
      ymax = 0.075
    } else {
      ymin = -0.05
      ymax = 0.05
    }
    
    
    ## Figure 3a: No Control: ONLY NO mw Increase
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      group_by(state_fips) %>%
      mutate(change = mean(mw_change_nom)) %>%
      filter(change==0 | treatyear>0)
    
    atts <- getatts_nohpi()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_nomw_nohpi"))
    
    ## Figure 3b:  ONLY NO mw Increase + Control for HPI
    lowerbound=0.5
    if (inc==50) { lowerbound=0.25 }
    
    atts <- getatts()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_nomw"))
    
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}




for (inc in c(50, 75, 100)) {
  tryCatch({
    label="Overall"
    
    xmin = -4
    xmax = 3
    if (inc==50) {
      ymin = -0.1
      ymax = 0.1
    } else if (inc==75) {
      ymin = -0.075
      ymax = 0.075
    } else {
      ymin = -0.05
      ymax = 0.05
    }
    
    
    ## Figure 4a: No Control
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) 
    atts <- getatts()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom"))
    
    ## Figure 4b: Control for smaller mn wage
    lowerbound=0.5
    if (inc==50) { lowerbound=0.25 }
    
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100 & treatyear==0, year, 9999)) %>%
      group_by(state_fips) %>%
      mutate(controlyear = min(controlyear)) %>%
      mutate(small = ifelse(year>=controlyear, 1,0)) %>%
      group_by(state_fips) %>%
      mutate(small = mean(small)) %>%
      mutate(small = ifelse(small>=0.5, 1,0)) 
    
    atts <- getatts_small2()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_small"))
    
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}





###############################################################################
#                           Dube: 1984-2013                                   #
###############################################################################



################## Load in the data ########################
data_analysis <- read.dta13("data/primary/state_panel_mw_poverty_84.dta") %>% 
  filter(year<=2013) %>%
  group_by(state_fips) %>% 
  mutate(dhpi=ifelse(chpi>=0.05,1,0)) %>%
  arrange(state_fips, year) %>%
  group_by(state_fips) %>%
  mutate(mw_change_nom = mw - lag(mw)) %>%
  mutate(mw_change_nom = ifelse(is.na(mw_change_nom),0,mw_change_nom)) %>%
  mutate(changed5 = ifelse(mw_change_nom >= 0.05, year, 9999)) %>%
  mutate(changed10 = ifelse(mw_change_nom >= 0.10, year, 9999)) %>%
  mutate(changed25 = ifelse(mw_change_nom >= 0.25, year, 9999)) %>%
  mutate(changed50 = ifelse(mw_change_nom >= 0.5, year, 9999)) %>%
  mutate(changed75 = ifelse(mw_change_nom >= 0.75, year, 9999)) %>%
  mutate(changed100 = ifelse(mw_change_nom >= 1.00, year, 9999)) %>%
  mutate(changed125 = ifelse(mw_change_nom >= 1.25, year, 9999)) %>%
  mutate(changed110 = ifelse(mw_change_nom >= 1.10, year, 9999)) %>%
  mutate(changed90 = ifelse(mw_change_nom >= 0.90, year, 9999)) %>%
  mutate(changed80 = ifelse(mw_change_nom >= 0.80, year, 9999)) %>%
  group_by(state_fips) %>% 
  mutate(eff_nom_5 = min(changed5)) %>%
  mutate(eff_nom_10 = min(changed10)) %>%
  mutate(eff_nom_25 = min(changed25)) %>%
  mutate(eff_nom_50 = min(changed50)) %>%
  mutate(eff_nom_75 = min(changed75)) %>%
  mutate(eff_nom_100 = min(changed100)) %>%
  mutate(eff_nom_80 = min(changed80)) %>%
  mutate(eff_nom_90 = min(changed90)) %>%
  mutate(eff_nom_110 = min(changed110)) %>%
  mutate(eff_nom_125 = min(changed125)) %>%
  mutate(y=under_100)


##################  RUN CS Estimates #############################


for (inc in c(10, 50, 75, 100)) {
  tryCatch({
    label="Overall"
    
    if (inc==50) {
      xmin = -4
      xmax = 5
      ymin = -0.3
      ymax = 0.3
    } 
    else if (inc==75) {
      xmin = -5
      xmax = 5
      ymin = -0.1
      ymax = 0.1
    }
    else {
      xmin = -5
      xmax = 5
      ymin = -0.05
      ymax = 0.05
    }
    
    ## Figure 5a: No Control
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) 
    atts <- getatts()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_8413"))
    
    ## Figure 5b: Control for smaller mn wage
    lowerbound=0.5
    if (inc==50) { lowerbound=0.25 }
    
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100 & treatyear==0, year, 9999)) %>%
      group_by(state_fips) %>%
      mutate(controlyear = min(controlyear)) %>%
      mutate(small = ifelse(year>=controlyear, 1,0)) %>%
      group_by(state_fips) %>%
      mutate(small = mean(small)) %>%
      mutate(small = ifelse(small>=0.5, 1,0)) 
    
    atts <-  getatts_small2()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_8413_small"))
    
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}











###############################################################################
#                             Full: 1984+                                     #
###############################################################################



################## Load in the data ########################
data_analysis <- read.dta13("data/primary/state_panel_mw_poverty_84.dta") %>%
  group_by(state_fips) %>% 
  mutate(dhpi=ifelse(chpi>=0.05,1,0)) %>%
  arrange(state_fips, year) %>%
  group_by(state_fips) %>%
  mutate(mw_change_nom = mw - lag(mw)) %>%
  mutate(mw_change_nom = ifelse(is.na(mw_change_nom),0,mw_change_nom)) %>%
  mutate(changed5 = ifelse(mw_change_nom >= 0.05, year, 9999)) %>%
  mutate(changed10 = ifelse(mw_change_nom >= 0.10, year, 9999)) %>%
  mutate(changed25 = ifelse(mw_change_nom >= 0.25, year, 9999)) %>%
  mutate(changed50 = ifelse(mw_change_nom >= 0.5, year, 9999)) %>%
  mutate(changed75 = ifelse(mw_change_nom >= 0.75, year, 9999)) %>%
  mutate(changed100 = ifelse(mw_change_nom >= 1.00, year, 9999)) %>%
  mutate(changed125 = ifelse(mw_change_nom >= 1.25, year, 9999)) %>%
  mutate(changed110 = ifelse(mw_change_nom >= 1.10, year, 9999)) %>%
  mutate(changed90 = ifelse(mw_change_nom >= 0.90, year, 9999)) %>%
  mutate(changed80 = ifelse(mw_change_nom >= 0.80, year, 9999)) %>%
  group_by(state_fips) %>% 
  mutate(eff_nom_5 = min(changed5)) %>%
  mutate(eff_nom_10 = min(changed10)) %>%
  mutate(eff_nom_25 = min(changed25)) %>%
  mutate(eff_nom_50 = min(changed50)) %>%
  mutate(eff_nom_75 = min(changed75)) %>%
  mutate(eff_nom_100 = min(changed100)) %>%
  mutate(eff_nom_80 = min(changed80)) %>%
  mutate(eff_nom_90 = min(changed90)) %>%
  mutate(eff_nom_110 = min(changed110)) %>%
  mutate(eff_nom_125 = min(changed125)) %>%
  mutate(y=under_100)


##################  RUN CS Estimates #############################
for (inc in c(50,75, 100)) {
  #for (inc in c(100)) {
  tryCatch({
    label="Overall"
    
    if (inc==50) {
      xmin = -4
      xmax = 5
      ymin = -0.3
      ymax = 0.3
    } else {
      xmin = -5
      xmax = 5
      ymin = -0.05
      ymax = 0.05
    }
    
    ## Figure 5a: No Control
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) 
    atts <- getatts()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_84"))
    
    ## Figure 5b: Control for smaller mn wage
    #lowerbound = (inc-25)/100
    lowerbound=0.5
    if (inc==50) { lowerbound=0.25 }
    
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100 & treatyear==0, year, 9999)) %>%
      group_by(state_fips) %>%
      mutate(controlyear = min(controlyear)) %>%
      mutate(small = ifelse(year>=controlyear, 1,0)) %>%
      group_by(state_fips) %>%
      mutate(small = mean(small)) %>%
      mutate(small = ifelse(small>=0.5, 1,0)) 
    
    atts <- getatts_small2()
    agg_effects_es <- aggte(atts, type = "dynamic", na.rm=T)
    plotcses_manual(paste0("overall_", inc, "_nom_84_small"))
    
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}






